package com.fmdj.mps.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.CommonResult;
import com.fmdj.mps.controller.form.CalculateDriveLineForm;
import com.fmdj.mps.service.CalculateTripDistanceAndDurationService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

@Service
public class CalculateTripDistanceAndDurationServiceImpl implements CalculateTripDistanceAndDurationService {

    //估算里程API地址
    private String distanceUrl = "https://apis.map.qq.com/ws/distance/v1/matrix/";
    //估算最佳行驶路线API
    private String directionUrl = "https://apis.map.qq.com/ws/direction/v1/driving/";

    @Value("${tencent.map.key}")
    private String key;

    @Override
    public HashMap calculateTripDistanceAndDuration(String mode, String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {

        //像distanceUrl发起请求
        HttpRequest request = new HttpRequest(distanceUrl);

        //设置参数
        request.form("mode", mode);
        request.form("from", startPlaceLatitude + "," + startPlaceLongitude);
        request.form("to", endPlaceLatitude + "," + endPlaceLongitude);
        request.form("key", key);

        //发送请求接受响应 并将响应转为json对象 方便获取数据
        HttpResponse response = request.execute();
        JSONObject jsonObject = JSONUtil.parseObj(response.body());

        //获取数据
        Integer status = jsonObject.getInt("status");
        Short message = jsonObject.getShort("message");

        //获取数据失败
        if (status != 0) {
            throw new GlobalException("在估算里程以及时间时出错");
        }

        //获取数据成功
        JSONArray rows = jsonObject.getJSONObject("result").getJSONArray("rows");
        JSONObject element = rows.get(0, JSONObject.class).getJSONArray("elements").get(0, JSONObject.class);
        Integer distance = element.getInt("distance");
        Integer duration = element.getInt("duration");

        //将米转成公里
        String mileage = new BigDecimal(distance).divide(new BigDecimal(1000)).toString();

        //将秒转成分钟
        String minute = new BigDecimal(duration).divide(new BigDecimal(60), 0, RoundingMode.CEILING).toString();

        //构造结果集
        HashMap hashMap = new HashMap() {{
            put("mileage", mileage);
            put("minute", Integer.parseInt(minute));
        }};

        return hashMap;
    }

    @Override
    public HashMap calculateBestDrivingLine(String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {

        //向directionUrl发起请求获取最佳行驶路线
        HttpRequest request = new HttpRequest(directionUrl);

        //设置参数
        request.form("from", startPlaceLatitude + "," + startPlaceLongitude);
        request.form("to", endPlaceLatitude + "," + endPlaceLongitude);
        request.form("key", key);

        //发送请求接受响应
        HttpResponse response = request.execute();

        //将响应转为json对象 方便获取数据
        JSONObject jsonObject = JSONUtil.parseObj(response.body());

        //解析响应数据
        JSONObject result = jsonObject.getJSONObject("result");

        //将resultJSON对象转化成hashMap对象
        HashMap bean = result.toBean(HashMap.class);
        return bean;
    }


}
